package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.ResponseInfo;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-16
 * Time: 下午3:47
 * To change this template use File | Settings | File Templates.
 */
public class KeywordcharacteristicAnalysis {

    private static Logger logger = LoggerFactory.getLogger(KeywordcharacteristicAnalysis.class);

    public static void main(String[] args) {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/query20131231.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath, "GBK");
        long startTime = System.currentTimeMillis();
        String baseUrl = "http://192.168.1.37:8098/analyze/?q=";
        String hospitalSearch = "http://10.10.100.153:9999/hospital_search?fl=doctor_uuid&rows=0&q=";
        String doctorSearch = "http://10.10.100.153:9999/doctor_search?fl=doctor_uuid&secondsort=0&rows=0&q=";
        MyHttpRequest httpRequest = new MyHttpClientRequest();
        System.out.println(keywordList.size());
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < keywordList.size(); i++){
            List<AnalyzeObject> objectList = new ArrayList<AnalyzeObject>();
            if(i % 1000 == 0){
                System.out.println("Deal with " + i + " keywords already.");
            }
            ResponseInfo hospitalResponseInfo = null;
            ResponseInfo doctorResponseInfo = null;
            try {
                objectList.addAll(JSONStreamParserForAnalyze.parse(baseUrl + URLEncoder.encode(keywordList.get(i), "UTF-8")));
                hospitalResponseInfo = httpRequest.requestAndGetResponseInfo(hospitalSearch + URLEncoder.encode(keywordList.get(i), "UTF-8"));
                doctorResponseInfo = httpRequest.requestAndGetResponseInfo(doctorSearch + URLEncoder.encode(keywordList.get(i), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            sb.append(keywordList.get(i)).append("\t");
            for(int j = 0; j < objectList.size(); j++){
                sb.append(objectList.get(j).getKey());
                if( j != objectList.size()-1){
                    sb.append(" ");
                }
            }
            sb.append("\t");
            for(int j = 0; j < objectList.size(); j++){
                sb.append(objectList.get(j).getType());
                if( j != objectList.size()-1){
                    sb.append(" ");
                }
            }
            sb.append("\t");
            sb.append(doctorResponseInfo.getReturnCount());
            sb.append("\t");
            sb.append(hospitalResponseInfo.getReturnCount());
            sb.append("\n");
        }
        logger.info(sb.toString());
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }

}
